const express = require('express') // express
const mongoose = require('mongoose') // mongose 数据库
const bodyParser = require('body-parser') // 请求体安装的库文件
const app = express() // 实例化了一个express服务器
const passport = require('passport') //登录的一个校验
const cors = require('cors') // 跨域问题的解决
const port = process.env.port || 5000 // 端口号

// 引入路由
const BaseURL = '/api'
const users = require('./routes/api/users')
const userlist = require('./routes/api/userlist')
const Food = require('./routes/api/Food')
const Cuisine= require('./routes/api/Cuisine')
const Seller= require('./routes/api/seller')
const invest = require('./routes/api/invest')
const package = require('./routes/api/package')
const upload = require('./routes/api/uploads')
// const uploadSeller = require('./routes/api/uploadSellerImag')

// DB config
const db = require('./config/keys').mongoURI

// 使用body-parser中间件
// 配置图片上传的大小
app.use(bodyParser.json({ limit: '500mb' }));
app.use(bodyParser.urlencoded({ limit: '500mb', extended: true }));
app.use(cors())

// DB connect
mongoose
  .connect(db, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('数据库连接成功')
  })
  .catch((e) => {
    console.log(e,'数据库连接失败')
  })

// 数据库修改和删除报错，需要添加这个
mongoose.set('useFindAndModify', false)
// passport 初始化
app.use(passport.initialize())
require('./config/passport')(passport)


app.use('/uploads', express.static('uploads'))


// 使用 routes
app.use(BaseURL, users)
app.use(BaseURL, userlist)
app.use(BaseURL, Food)
app.use(BaseURL, Seller)
app.use(BaseURL, Cuisine)
app.use(BaseURL, invest)
app.use(BaseURL, package)
app.use(BaseURL, upload)
// app.use(BaseURL, uploadSeller)


app.listen(port, () => {
  console.log(`Server running in port ${port}`)
})
